﻿using DLL.NET70.Infrastructure;
using DLL.NET70.Infrastructure.EventAggregator;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Entity;
using DLL.Standard.Infrastructure.Log;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Interop;

namespace Module.MDViews.ViewModels
{
    public class Drawer_LogInfosViewModel : CMVVMBase
    {
        private IEventAggregator _eventAggregator;

        public Drawer_LogInfosViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) : base(regionManager, eventAggregator)
        {
            _eventAggregator = eventAggregator;
        }

        public override void Loaded()
        {
            RegisterLogMsgReceive();
            RegisterCommandMsgReceive();
            base.Loaded();
        }

        public override void UnLoaded()
        {
            UnRegisterLogMsgReceive();
            UnRegisterCommandMsgReceive();
            base.UnLoaded();
        }

        public override void ReceiveLogMsg(CLogMessage Message)
        {
            Application.Current.Dispatcher.Invoke(
            (Action)delegate ()
            {
                // do your UI work here
                AddLogMsg(Message.Message, (LogLevel)Message.Level);
            });
            base.ReceiveLogMsg(Message);
        }

        public override void CommandMessageReceived(CCommandMessage message)
        {
            if (message.CommandStr == GlobalCommand.ClearLogMsg)
            {
                ListItems.Clear();
            }
            base.CommandMessageReceived(message);
        }

        private ObservableCollection<logInfo> _listItems = new ObservableCollection<logInfo>();

        public ObservableCollection<logInfo> ListItems
        {
            get { return _listItems; }
            set { SetProperty(ref _listItems, value); }
        }

        private void AddLogMsg(string msg, LogLevel level = 0)
        {
            logInfo logInfo = new logInfo();
            logInfo.Info = msg;
            switch (level)
            {
                case 0:
                    {
                        logInfo.backGround = "LimeGreen";
                        break;
                    }

                case LogLevel.Debug:
                    {
                        logInfo.backGround = "LimeGreen";
                        break;
                    }
                case LogLevel.Information:
                    {
                        logInfo.backGround = "LimeGreen";
                        break;
                    }
                case LogLevel.Warning:
                    {
                        logInfo.backGround = "Orange";
                        break;
                    }
                case LogLevel.Error:
                    {
                        logInfo.backGround = "Red";
                        break;
                    }

                default:
                    {
                        logInfo.backGround = "LimeGreen";
                        break;
                    }
            }
            _listItems.Add(logInfo);
            LogHelper.Debug(logInfo.Info);
        }
    }
}